<?php


namespace app\common\server;

// use extend\oss\autoload;
use OSS\OssClient;
use OSS\Core\OssUtil;
use OSS\Core\OssException;

require_once '../extend/oss/autoload.php';


class OssUp
{

    private $accessKeyId = '';
    private $accessKeySecret = '';
    private $endpoint = '';
    private $bucket = '';
    private $ossClient = '';

    public function __construct()
    {
        // OSS配置
        $this->accessKeyId = 'LTAI5tRJHd6Uj8J2eWDssf11';
        $this->accessKeySecret = 'zrfh3ZrmSAvf0ZJAiSUKoMU5LhEQfX';
        $this->endpoint = 'http://oss-cn-beijing.aliyuncs.com';
        $this->bucket = 'ouhechaowan';
        
        $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
    }

    /**
     * @param string $object
     * @param string $uploadFile
     * @return string|void
     * @throws \OSS\Http\RequestCore_Exception
     */
    public function upload_ ($object = '', $uploadFile = '')
    {
        $initOptions = array(
            OssClient::OSS_HEADERS  => array(

            ),
        );

        try{
            $uploadId = $this->ossClient->initiateMultipartUpload($this->bucket, $object, $initOptions);
        } catch(OssException $e) {
            printf($e->getMessage() . "\n");
            return;
        }



        $partSize = 1 * 1024 * 1024;
        $uploadFileSize = sprintf('%u',filesize($uploadFile));
        $pieces = $this->ossClient->generateMultiuploadParts($uploadFileSize, $partSize);
        $responseUploadPart = array();
        $uploadPosition = 0;
        $isCheckMd5 = false;
        foreach ($pieces as $i => $piece) {
            $fromPos = $uploadPosition + (integer)$piece[$this->ossClient::OSS_SEEK_TO];
            $toPos = (integer)$piece[$this->ossClient::OSS_LENGTH] + $fromPos - 1;
            $upOptions = array(
                // 上传文件。
                $this->ossClient::OSS_FILE_UPLOAD => $uploadFile,
                // 设置分片号。
                $this->ossClient::OSS_PART_NUM => ($i + 1),
                // 指定分片上传起始位置。
                $this->ossClient::OSS_SEEK_TO => $fromPos,
                // 指定文件长度。
                $this->ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
                // 是否开启MD5校验，true为开启。
                $this->ossClient::OSS_CHECK_MD5 => $isCheckMd5,
            );
            // 开启MD5校验。
            if ($isCheckMd5) {
                $contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
                $upOptions[$this->ossClient::OSS_CONTENT_MD5] = $contentMd5;
            }
            try {
                // 上传分片。
                $responseUploadPart[] = $this->ossClient->uploadPart($this->bucket, $object, $uploadId, $upOptions);
            } catch(OssException $e) {
                printf("initiateMultipartUpload, uploadPart - part#{$i} FAILED\n");
                printf($e->getMessage() . "\n");
                return;
            }

        }
        $uploadParts = array();
        foreach ($responseUploadPart as $i => $eTag) {
            $uploadParts[] = array(
                'PartNumber' => ($i + 1),
                'ETag' => $eTag,
            );
        }
 
        $comOptions['headers'] = array(

        );
        try {
            $res = $this->ossClient->completeMultipartUpload($this->bucket, $object, $uploadId, $uploadParts,$comOptions);
            return $object;
        }  catch(OssException $e) {
            printf("Complete Multipart Upload FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

    }

}